package com.xw.service.imp;

import com.github.pagehelper.PageHelper;
import com.xw.common.Const;
import com.xw.dao.BuyVideosMapper;
import com.xw.dao.VideosInfoMapper;
import com.xw.model.BuyVideos;
import com.xw.model.VideosInfo;
import com.xw.service.IVideosInfoService;
import com.xw.util.AppUtil;
import com.xw.util.JsonData;
import com.xw.vo.VideosInfoVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @需求描述:
 * @创建人:whh
 * @创建时间: 2019/11/11
 * @修改需求:
 * @修改人:
 * @修改时间:
 * @需求思路:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class VideosInfoService implements IVideosInfoService {

    @Resource
    private VideosInfoMapper videosInfoMapper;
    @Resource
    private BuyVideosMapper buyVideosMapper;

    /**
     * @需求描述:资料信息业务逻辑层
     * @创建人:whh
     * @创建时间: 2019/11/13
     * @修改需求:
     * @修改人:
     * @修改时间:
     * @需求思路:
     */
    @Override
    public Map<String, Object> showVideoInfo(Map<String, Object> map) {
        VideosInfoVo videosInfoVo = (VideosInfoVo)map.get("videosInfoVo");
        if (null == videosInfoVo.getVideoId() || "" == videosInfoVo.getVideoId()){
            return JsonData.fail(Const.PARA_IS_NULL);
        }

        PageHelper.orderBy("video_info_sort asc");
        List<VideosInfoVo> videosInfoVos =  videosInfoMapper.select(videosInfoVo);
        map.clear();
        map.put("videosInfoVos",videosInfoVos);

        return JsonData.success(map,Const.OPREATE_MSG_SUCCESS);
    }

    @Override
    public Object insertVideoInfo(String userId, String videoId, Integer sort, String videoInfoName, String videoInfoUrl) {
        VideosInfoVo videosInfoVo = new VideosInfoVo();
        videosInfoVo.setCreateId(userId);
        videosInfoVo.setCreateTime(new Date());
        videosInfoVo.setVideoInfoName(videoInfoName);
        videosInfoVo.setVideoInfoSort(1);
        videosInfoVo.setVideoId(videoId);
        videosInfoVo.setVideoInfoUrl(videoInfoUrl);
        videosInfoVo.setUpdateTime(new Date());
        videosInfoVo.setVideosInfoId(AppUtil.uuid());
        videosInfoVo.setDownCount(0);
        int insert = videosInfoMapper.insertSelective(videosInfoVo);
        if (1 == insert){
            return  JsonData.success(Const.OPREATE_MSG_SUCCESS);
        }

        return JsonData.fail(Const.OPREATE_MSG_ERROR);
    }


    /**
     * @需求描述:下载资料
     * @创建人:whh
     * @创建时间: 2019/11/29
     * @修改需求:
     * @修改人:
     * @修改时间:
     * @需求思路:
     */
    @Override
    public Map<String, Object> downLoadVideo(Map<String, Object> map) {
        VideosInfoVo videosInfoVo = (VideosInfoVo)map.get("videosInfoVo");
        String userId = (String)map.get("userId");
        if (null == videosInfoVo.getVideosInfoId() || "" == videosInfoVo.getVideosInfoId()){
            return JsonData.fail(Const.PARA_IS_NULL);
        }
        VideosInfoVo videosInfoVos =  videosInfoMapper.selectOne(videosInfoVo);
        BuyVideos buyVideos = new BuyVideos();
        buyVideos.setVideosId(videosInfoVos.getVideoId());
        buyVideos.setUserId(userId);
        Integer count = buyVideosMapper.selectCount(buyVideos);
        if (count == 0 ){
            return JsonData.fail("对不起，请求失败，请先购买课程！");
        }

        PageHelper.orderBy("video_info_sort asc");

        videosInfoVos.setDownCount(videosInfoVos.getDownCount()+1);
        videosInfoMapper.updateByPrimaryKey(videosInfoVos);
        map.clear();
        map.put("url",videosInfoVos.getVideoInfoUrl());

        return JsonData.success(map);
    }
}



